package com.example.usb.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.usb.R;
import com.example.usb.dao.util.FaceDaoUtil;
import com.lib.uvccamera.bean.FaceBean;
import com.lib.uvccamera.bean.MessageEvent;
import com.lib.uvccamera.utils.DebugLog;

import org.greenrobot.eventbus.EventBus;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.lib.uvccamera.utils.Aiconstant.SCREEN_FINISH;
import static com.lib.uvccamera.utils.Aiconstant.SCREEN_OK;
import static com.lib.uvccamera.utils.DebugLog.LOGHsc;

/**
 * created by liangkun on ${DATA}
 * Describe:检测到人脸的适配器
 */

public class FaceDetectAdapter extends RecyclerView.Adapter<FaceDetectAdapter.ViewHolder> {
    private Context context;
    private List<FaceBean> faceList;

    public FaceDetectAdapter(Context context, List<FaceBean> faceList) {
        this.context = context;
        this.faceList = faceList;
    }

    public void updateHead(List<FaceBean> list) {
        faceList.clear();
        this.faceList.addAll(list);
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public FaceDetectAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_detect_face, viewGroup, false);
        return new FaceDetectAdapter.ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull FaceDetectAdapter.ViewHolder viewHolder, int postion) {
        if (faceList != null) {

            if (faceList.size() > 0) {
                if (faceList.get(postion) != null) {
                    DebugLog.d(LOGHsc + " 人脸数目*****************---- " + faceList.get(postion).getHeadPaht());
                    String finger = faceList.get(postion).getFirger();
                    String headPath = FaceDaoUtil.getInstance().getHeadPathByFigure(finger);
                    String newHeadPath = faceList.get(postion).getHeadPaht();
                    if (!TextUtils.isEmpty(headPath)) {
                        double matchSoce = faceList.get(postion).getMatchScore();
                        if (matchSoce == 0d) {
                            DebugLog.d(LOGHsc + " 人脸数目*******matchSoce == 0d---- " + faceList.size());
                            //隐藏
                            setImage(newHeadPath, viewHolder.ivNew);
                        } else {
                            //TODO:分数太长取2位
                            DebugLog.d(LOGHsc + " 人脸数目*******-matchSoce不为0d--- " + faceList.size());

                            setImage(newHeadPath, viewHolder.ivNew);
                            setImage(headPath, viewHolder.ivOld);
                            DecimalFormat df = new DecimalFormat(".00");
                            String format = df.format(matchSoce);
                            viewHolder.tvScore.setText(format);
                        }
                    } else {
                        viewHolder.llContainer.setVisibility(View.GONE);
                    }
                }
            }
        }


    }

    @Override
    public int getItemCount() {
        if (faceList != null) {
            return faceList.size();
        }
        return 0;
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.llContainer)
        LinearLayout llContainer;
        @BindView(R.id.iv_new)
        ImageView ivNew;
        @BindView(R.id.iv_old)
        ImageView ivOld;
        @BindView(R.id.llScoe)
        LinearLayout llScore;
        @BindView(R.id.tv_score)
        TextView tvScore;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 解析图片
     *
     * @param url       地址
     * @param imageView 图片
     */
    private void loadUrl(String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .asBitmap()
                .centerCrop()
                .placeholder(R.mipmap.face_defult)
                .error(android.R.drawable.ic_menu_report_image).listener(new RequestListener<String, Bitmap>() {
            @Override
            public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                Glide.with(context)
                        .load(url)
                        .asBitmap()
                        .centerCrop()
                        .placeholder(R.mipmap.face_defult)
                        .into(imageView);
                return false;
            }

            @Override
            public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
                return false;
            }
        }).into(imageView);
    }

    /**
     * 显示图片
     *
     * @param headPath  人脸图片
     * @param imageView 控件
     */
    public void setImage(String headPath, ImageView imageView) {
        Bitmap bm = BitmapFactory.decodeFile(headPath);
        imageView.setImageBitmap(bm);
    }
}
